<template>
  <div class="iot">
    <div>
      <label>{{$t('route.DeviceManager')}}</label>
      <div></div>
      <button><img /><label>{{$t('route.IOT_data')}}</label></button>
    </div>

    <div>
      <el-radio-group @change="getDeviceProList" v-model="dataType">
        <el-radio-button :label="$t('route.energy_consumption')"></el-radio-button>
        <el-radio-button :label="$t('route.Crop_rate')"></el-radio-button>
        <el-radio-button :label="$t('route.Working_hours')"></el-radio-button>
      </el-radio-group>

      <div>
        {{ device.kind }} {{ device.noseCode }}
      </div>
      <div>
        <!-- <el-select @change="getDeviceProList" style="float:right;" v-model="monthday" placeholder="请选择月份">
          <el-option v-for="item in months" :key="item" :label="item" :value="item">
          </el-option>
        </el-select> -->
        <el-date-picker style="float:right;" @change="getDeviceProList" v-model="dataMonth" type="month"
          :placeholder="$t('route.Select_date')" />
      </div>

    </div>
    <div style="margin-top: 20px;">
      <div v-if="chartShow" id="myChart" style="height: 250px;width: 100%;"></div>
    </div>
    <div>
      <el-table :data="tableData" :row-style="{ height: '60px' }">
        <el-table-column prop="createTime" :label="$t('route.date')" sortable />
        <el-table-column prop="electric" :formatter="formatele" :label="$t('route.Power_consumption')" />
        <el-table-column prop="Ratio" :formatter="formatrat" :label="$t('route.Crop_rate')" />
        <el-table-column prop="workDur" :formatter="formatwork" :label="$t('route.Working_hours')" />
        <el-table-column prop="leisurem" :formatter="formatlei" :label="$t('route.Idle_time')" />
        <el-table-column prop="bootUpDur" :formatter="formatboot" :label="$t('route.Startup_duration')" />
      </el-table>
    </div>
  </div>
</template>
  
<script>
import http from "../../js/http/http.js";
import api from "../../js/http/api.js";
import CommonUtils from "../../js/common/common-utils.js";
import * as echarts from "echarts";
import i18n from '../../i18n/index';
export default {
  data() {
    return {
      option: {},
      dataType: i18n.global.t('route.energy_consumption'),
      xdata: [],
      ydata: [],
      device: {},
      tableData: [],
      dataMonth: new Date(),
      chartShow: false,
    };
  },
  methods: {
    // dataTypeChange() {
    //   this.getDeviceProList();
    // },
    getDeviceProList() {
      http.post(
        api.GET_DEVICE_PROLIST2, {
        month: this.dataMonth.format('yyyy-MM'),
        deviceId: this.device.id,
      }, data => {
        this.tableData = data.data;
        var xData = [];
        var yData = [];
        for (var item of data.data) {
          xData.push(item.createTime);
          if (this.dataType == i18n.global.t('route.energy_consumption')) {
            yData.push(item.electric.toFixed(2));
          } else if (this.dataType == i18n.global.t('route.Crop_rate')) {
            item.bootUpDur == 0 ? yData.push(0) : yData.push(
              ((item.workDur / item.bootUpDur) * 100).toFixed(1)
            );
          } else {
            yData.push(Number(item.workDur / 3600).toFixed(1));
          }
        }
        var myChart = echarts.init(document.getElementById("myChart"));
        var option = null;
        if (this.dataType == i18n.global.t('route.energy_consumption')) {
          option = CommonUtils.initBarOption(i18n.global.t('route.energy_consumption'), xData, yData);
        } else if (this.dataType == i18n.global.t('route.Crop_rate')) {
          option = CommonUtils.initBarOption(i18n.global.t('route.Crop_rate'), xData, yData);
        } else {
          option = CommonUtils.initBarOption(i18n.global.t('route.Working_hours'), xData, yData);
        }
        option.series[0].realtimeSort = false;
        myChart.setOption(option);

      }
      );
    },

    formatlei(row) {
      return CommonUtils.formatDur(row.bootUpDur - row.workDur);
    },
    formatboot(row) {
      return CommonUtils.formatDur(row.bootUpDur);
    },
    formatwork(row) {
      return CommonUtils.formatDur(row.workDur);
    },
    formatrat(row) {
      return ((row.workDur / row.bootUpDur) * 100).toFixed(1) + '%';
    },
    formatele(row) {
      return row.electric.toFixed(1) + 'kW·h';
    },


  },
  created() {
    this.chartShow = true;
    this.device = JSON.parse(CommonUtils.getParam(this, "device", "setDevice"));
    this.getDeviceProList();

  },
  destroyed() {
    this.chartShow = false;
  }
};
</script>
  
 
<style lang="scss" scoped>
.iot {
  color: #444444;
  font-size: 18px;
  padding-right: 20px;
  height: calc(100vh - 100px);
  // overflow: scroll;
  scrollbar-width: none;
  /* firefox */
  -ms-overflow-style: none;
  /* IE 10+ */
  overflow-x: hidden;
  overflow-y: auto;

  >div {
    display: flex;
    margin-top: 20px;
    background-color: #ffffff;
    padding: 20px;
    border-radius: 5px;
  }

  >div:nth-of-type(1) {
    display: flex;
    flex-direction: row;
    align-items: center;
    margin-top: 20px;
    background-color: #ffffff;
    padding: 20px;
    border-radius: 5px;

    >label:nth-of-type(1) {
      font-weight: 600;
    }

    >div:nth-of-type(1) {
      width: 1px;
      background-color: #cccccc;
      height: 15px;
      margin: 0px 20px;
    }

    >button:nth-of-type(1) {
      background-image: url(../../assets/img/customer-1.png);
      background-repeat: no-repeat;
      background-size: 100% 100%;
      padding: 5px 10px;
      border: 0px;
      display: flex;
      color: #ffffff;
      flex-direction: row;
      align-items: center;
      font-weight: 800;

      >img {
        margin-right: 7px;
        width: 15px;
        height: 15px;
        content: url(../../assets/img/customer-2.png);
      }
    }
  }

  >div:nth-of-type(2) {
    display: flex;
    flex-direction: row;

    >div {
      flex: 1;
      text-align: center;
    }
  }

  >div:nth-of-type(3) {
    display: flex;
    flex-direction: column;
    margin-top: 20px;
    background-color: #ffffff;
    padding: 20px;
    border-radius: 5px;

    >div:nth-of-type(1) {
      display: flex;
      flex-direction: row;
      align-items: center;

      >div:nth-of-type(1) {
        display: flex;
        flex-direction: row;
        align-items: center;
      }

      .div-handle-right {
        flex: 1;
        flex-direction: row-reverse;
        text-align: right;
        display: flex;
        align-items: center;

        >input:nth-of-type(1) {
          border: 1px solid #eeeeee;
          font-size: 15px;
          width: 150px;
          padding: 5px 8px;
          outline: none;
        }

        >button {
          color: #a866ff;
          margin-left: 10px;
          padding: 4px 10px;
        }
      }

      >div:nth-of-type(2),
      >div:nth-of-type(3) {
        margin-left: 10px;
      }
    }


  }


}
</style>